package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.entity.*;
import com.example.service.IProductHealthItemService;
import com.example.service.IProductHealthService;
import com.example.service.IProductService;
import com.example.service.IProductSwiperImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品Controller
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private IProductSwiperImageService productSwiperImageService;

    @Autowired
    private IProductHealthService productHealthService;
    @Autowired
    private IProductHealthItemService productHealthItemService;
    /**
     * 查询轮播商品
     * @return
     */
    @GetMapping("/findSwiper")
    public R findSwiper(){
        List<Product> swiperProductList = productService.list(new QueryWrapper<Product>().eq("isSwiper", true).orderByAsc("swiperSort"));
        Map<String,Object> map=new HashMap<>();
        map.put("message",swiperProductList);
        return R.ok(map);
    }

    /**
     * 查询热门推荐商品8个
     * @return
     */
    @GetMapping("/findHot")
    public R findHot(){
        Page<Product> page=new Page<>(0,8);
        Page<Product> pageProduct = productService.page(page, new QueryWrapper<Product>().eq("isHot", true).orderByAsc("hotDateTime"));
        List<Product> hotProductList = pageProduct.getRecords();
        Map<String,Object> map=new HashMap<>();
        map.put("message",hotProductList);
        return R.ok(map);
    }
    /**
     * 查询热门推荐健康套餐商品4个
     * @return
     */
    @GetMapping("/findHotHealth")
    public R findHotHealth() {
        Page<ProductHealth> page = new Page<>(0, 2);
        // 使用 QueryWrapper 执行多表查询
        List<ProductHealth> hotProductHealthList = productHealthService.page(page, new QueryWrapper<ProductHealth>()
                .eq("isHot", true)
                .orderByDesc("hotDateTime")
        ).getRecords();
        // 加载产品项信息
        for (ProductHealth health : hotProductHealthList) {
            List<ProductHealthItem> items = productHealthItemService.list(new QueryWrapper<ProductHealthItem>()
                    .eq("healthId", health.getId()));
            for (ProductHealthItem item : items) {
                Product product = productService.getById(item.getGoodId()); // 根据商品ID查询商品信息
                item.setProduct(product); // 关联产品信息
            }
            health.setProductHealthItems(items); // 设置健康套餐的产品项
        }
        Map<String, Object> map = new HashMap<>();
        map.put("message", hotProductHealthList);
        return R.ok(map);
    }
    /**
     * 查询所有健康套餐商品
     */
    @GetMapping("/findAllHotHealth")
    public R findAllHotHealth() {
        // 查询所有健康套餐
        List<ProductHealth> allProductHealthList = productHealthService.list();

        // 加载产品项信息
        for (ProductHealth health : allProductHealthList) {
            List<ProductHealthItem> items = productHealthItemService.list(new QueryWrapper<ProductHealthItem>()
                    .eq("healthId", health.getId()));
            for (ProductHealthItem item : items) {
                Product product = productService.getById(item.getGoodId()); // 根据商品ID查询商品信息
                item.setProduct(product); // 关联产品信息
            }
            health.setProductHealthItems(items); // 设置健康套餐的产品项
        }

        Map<String, Object> map = new HashMap<>();
        map.put("message", allProductHealthList);
        return R.ok(map);
    }

    @GetMapping("/detail")
    public R detail(Integer id){
        Product product = productService.getById(id);
        List<ProductSwiperImage> productSwiperImageList = productSwiperImageService.list(new QueryWrapper<ProductSwiperImage>().eq("productId", product.getId()).orderByAsc("sort"));
        product.setProductSwiperImageList(productSwiperImageList);
        Map<String,Object> map=new HashMap<>();
        map.put("message",product);
        return R.ok(map);
    }
    //健康详情
    @GetMapping("/detailHealth")
    public R healthDetail(Integer id) {
        // 根据ID获取ProductHealth
        ProductHealth productHealth = productHealthService.getById(id);
        // 如果productHealth不为空，继续获取相关项目和图片
        if (productHealth != null) {
            // 获取与此ProductHealth相关的ProductHealthItems列表
            List<ProductHealthItem> items = productHealthItemService.list(
                    new QueryWrapper<ProductHealthItem>().eq("healthId", productHealth.getId())
            );
            // 对于每个ProductHealthItem，获取关联的Product
            for (ProductHealthItem item : items) {
                Product product = productService.getById(item.getGoodId());
                item.setProduct(product); // 将产品与项目关联
            }
            // 在ProductHealth中设置ProductHealthItems列表
            productHealth.setProductHealthItems(items);
            // 获取健康套餐中产品的轮播图片
            List<ProductSwiperImage> productSwiperImageList = productSwiperImageService.list(
                    new QueryWrapper<ProductSwiperImage>()
                            .in("productId", items.stream().map(ProductHealthItem::getGoodId).collect(Collectors.toList()))
                            .orderByAsc("sort")
            );
            // 在ProductHealth中设置轮播图片
            productHealth.setProductSwiperImageList(productSwiperImageList);
        }
        // 准备响应数据
        Map<String, Object> map = new HashMap<>();
        map.put("message", productHealth);
        // 返回响应
        return R.ok(map);
    }



    /**
     * 商品搜索
     * @return
     */
    @GetMapping("/search")
    public R search(String q){
        List<Product> productList = productService.list(new QueryWrapper<Product>().like("name", q));
        Map<String,Object> map=new HashMap<>();
        map.put("message",productList);
        return R.ok(map);
    }
}
